package com.lhd.qd.module.system.dict.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhd.qd.common.base.BasePageQuery;
import com.lhd.qd.common.base.BaseServiceImpl;
import com.lhd.qd.common.exception.BusinessException;
import com.lhd.qd.module.system.dict.dao.DictMapper;
import com.lhd.qd.module.system.dict.model.dataobj.DictDO;
import com.lhd.qd.module.system.dict.model.dto.DictSaveDTO;
import com.lhd.qd.module.system.dict.model.dto.DictUpdateDTO;
import com.lhd.qd.module.system.dict.model.vo.DictDetailVO;
import com.lhd.qd.module.system.dict.model.vo.DictListVO;
import com.lhd.qd.module.system.dict.model.converter.AbstractDictConverter;
import com.lhd.qd.module.system.dict.service.DictCacheService;
import com.lhd.qd.module.system.dict.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class DictServiceImpl extends BaseServiceImpl<DictMapper, DictDO> implements DictService {

    @Autowired
    private DictCacheService dictCacheService;

    @Override
    public IPage<DictListVO> pageDict(String typeCode, BasePageQuery query) {

        LambdaQueryWrapper<DictDO> queryWrapper = Wrappers.<DictDO>lambdaQuery()
                .eq(DictDO::getTypeCode, typeCode);

        IPage<DictDO> doPage = page(new Page<>(query.getCurrent(), query.getSize()), queryWrapper);

        return AbstractDictConverter.INSTANCE.doPage2ListVOPage(doPage);
    }

    @Override
    public DictDetailVO getDictById(String typeCode, Integer code) {

        LambdaQueryWrapper<DictDO> queryWrapper = Wrappers.<DictDO>lambdaQuery()
                .eq(DictDO::getTypeCode, typeCode)
                .eq(DictDO::getCode, code);
        DictDO dataObj = getOne(queryWrapper);

        return AbstractDictConverter.INSTANCE.do2DetailVO(dataObj);
    }

    @Override
    public boolean saveDict(String typeCode, DictSaveDTO saveDTO) {

        LambdaQueryWrapper<DictDO> queryWrapper = Wrappers.<DictDO>lambdaQuery()
                .eq(DictDO::getTypeCode, typeCode)
                .eq(DictDO::getCode, saveDTO.getCode());
        int count = count(queryWrapper);
        if (count > 0) {
            throw new BusinessException("字典编码已存在，请更换。");
        }

        DictDO dataObj = AbstractDictConverter.INSTANCE.saveDTO2DO(saveDTO);
        dataObj.setTypeCode(typeCode);

        boolean isSuccess = save(dataObj);

        dictCacheService.refreshCache(typeCode);

        return isSuccess;
    }

    @Override
    public boolean updateDictById(String typeCode, Integer code, DictUpdateDTO updateDTO) {

        LambdaUpdateWrapper<DictDO> updateWrapper = Wrappers.<DictDO>lambdaUpdate()
                .eq(DictDO::getTypeCode, typeCode)
                .eq(DictDO::getCode, code);

        DictDO dataObj = AbstractDictConverter.INSTANCE.updateDTO2DO(updateDTO);

        boolean isSuccess = update(dataObj, updateWrapper);

        dictCacheService.refreshCache(typeCode);

        return isSuccess;
    }

    @Override
    public boolean removeDictById(String typeCode, Integer code) {

        LambdaUpdateWrapper<DictDO> updateWrapper = Wrappers.<DictDO>lambdaUpdate()
                .eq(DictDO::getTypeCode, typeCode)
                .eq(DictDO::getCode, code);

        boolean isSuccess = remove(updateWrapper);

        dictCacheService.refreshCache(typeCode);

        return isSuccess;
    }

    @Override
    public boolean removeDictByTypeCode(String typeCode) {

        LambdaUpdateWrapper<DictDO> updateWrapper = Wrappers.<DictDO>lambdaUpdate()
                .eq(DictDO::getTypeCode, typeCode);

        return remove(updateWrapper);
    }
}
